/*
 * @description 管理员的路由 API 接口
 * @Author: Eve 
 * @Date: 2021-09-09 14:11:12 
 * @Last Modified by: Eve
 * @Last Modified time: 2021-09-10 18:10:21
 */

const router = require('koa-router')()
router.prefix('/api/v1/admin')
const {Resolve} = require('../../lib/helper');
const res = new Resolve();

const {LoginManager} = require('../../service/login');
const {
    RegisterValidator, AdminLoginValidator,
  } = require('../../validators/admin')
const {AdminController} = require('../../controller/admin')
router.post('/project', async (ctx) => {
   console.log('44')
});
// 管理员注册
router.post('/register', async (ctx) =>{
       // 通过验证器校验参数是否通过
    const v = await new RegisterValidator().validate(ctx);

    await AdminController.create({
        email:v.get('body.email'),
        password: v.get('body.password2'),
        username: v.get('body.username')
    })
    // 返回结果
    ctx.response.status = 200;
    const success = {
        msg:'注册成功'
    }
    ctx.body = res.json(success);
})

// 管理登录
router.post('/login', async (ctx) =>{
    const v = await new AdminLoginValidator().validate(ctx);
    let token = await LoginManager.adminLogin({
        email: v.get('body.email'),
        password: v.get('body.password')
      });
      // 返回结果
      ctx.response.status = 200;
      ctx.body = {
        code: 200,
        msg: '登录成功',
        token
      }
})

router.get('/list', async (ctx) => {
    const list = await AdminController.list();
    // 返回结果
    ctx.response.status = 200;
    ctx.body = res.json(list);
})

module.exports = router